package rpa

import (
	"context"
	"errors"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/rpa"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/rpa"
	"git.myscrm.cn/golang/stark/v4"
)

/**
 * @Author: xiuliu
 * @Description:
 * @File:  rpa_config_server
 * @Version: 1.0.0
 * @Date: 2023/10/30 17:52
 */

type RpaConfigServer struct {
	rpaService rpa.RpaServiceIface
}

func NewRpaConfigServer(rpaService rpa.RpaServiceIface) pb.RpaConfigServiceServer {
	return &RpaConfigServer{
		rpaService: rpaService,
	}
}

func (r *RpaConfigServer) SaveRpaConfig(ctx context.Context, request *pb.SaveRpaConfigRequest) (*pb.SaveRpaConfigResponse, error) {
	if request.GetData().UserMobile == "" {
		return nil, errors.New("手机号参数不能为空")
	}

	secretKey, err := r.rpaService.SaveData(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "保存RPA配置信息出错", err.Error())
		return nil, err
	}
	resp := &pb.SaveRpaConfigResponse{
		SecrectKey: secretKey,
	}
	return resp, nil
}

func (r *RpaConfigServer) GetRpaConfigInfo(ctx context.Context, request *pb.GetRpaConfigInfoRequest) (*pb.GetRpaConfigResponse, error) {
	if request.UserMobile == "" {
		return nil, errors.New("手机号参数不能为空")
	}
	resp, err := r.rpaService.GetInfo(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "获取RPA配置信息出错", err.Error())
		return nil, err
	}

	return resp, nil
}
